<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<title>Insert title here</title>
<script type="text/javascript">
$(document).ready(function(){
	
	
	$("#writeBtn").on("click",function(){
		var title = $("#title").val();
		if(title==""){
			alert("제목을 입력해주세요.");
			return;
		}		
		var content = $("#content").val();
		if(content==""){
			alert("내용를 입력해주세요.");
			return;
		}		
		var flag=confirm("글작성을 완료 하시겠습니까?");		
		 if(flag){
			 var f = $("#write_form");
			 f.attr("action","${initParam.root }/board.do?mode=write");
			 f.submit();
		 }		
	});	
});



function reviewUploadImg(fileObj)
{
 var filePath = fileObj.value;
 var fileName = filePath.substring(filePath.lastIndexOf("\\")+1);
 var fileKind = fileName.split(".")[1];
 if(fileKind != "jpg" && fileKind != "gif" && fileKind != "png")
 {
  alert("jpg, gif, png 확장자를 가진 이미지 파일만 올려주세요.");
  document.getElementById("image_file").value = "";
  document.getElementById("image_file").select();
  document.selection.clear();
 }else{
	
	 
	 var uploadData = $("#uploadFile").prop("files")[0];
	 var newData = new FormData();

	 $.each($('#uploadFile').prop("files"), function(i, file) {
	     newData.append('uploadFile', file);
	 });
	 newData.append("mode","thumbImg");
	 $.ajax({

	     type        : 'POST',
	     url     : '${initParam.root }/board.do?mode=thumbImg',
	     data        : newData,
	     contentType: false,
	     cache: false,
	     processData: false,                        
	     success     : function(r) {
	    	 alert(r.img);
	    	 var i = "<img src='${initParam.root }/thumb/";
	    	 i+=r.img;
	    	 i+="'></img>";
	         $("#previewImg").html(i);
	         

	     },

	     error       : function(r) { alert('jQuery Error'); }

	 });
	 
 }
}

</script>
</head>
<body>
<c:choose>
	<c:when test="${sessionScope.mvo.uId==null }">
	<script type="text/javascript">
			alert("로그인을 하셔야합니다!");
			location.href="${initParam.root }/index.jsp";
		</script>
	</c:when>

<c:otherwise>
<font face="HY나무L" size="5"><strong>글쓰기 <br><br></strong></font>

  <form action="${initParam.root }/board.do" method="post" id="write_form" enctype="multipart/form-data">
  <!-- <input type="hidden" name="mode" value="write"> -->
  <input type="hidden" name="category" value="${param.category }">
  

   <table width="70%" align="center">
    <tr>
     <td width="10%">제목</td>
     <td colspan="3">
     <input type="text" id="title" maxlength="200" size="50" name="title">
     </td>
    </tr>
    <tr>
    	<td colspan="4">
    	<input type="file" name="uploadFile" id="uploadFile" size="60"  onchange="reviewUploadImg(this);"/>
    	</td>
    </tr>
    <tr>
    	<td colspan="4">
    		<div id="previewImg"></div>
    	</td>	
    </tr>

    <tr>

     <td colspan="4">
     <textarea cols="80" rows="20" id="content" name="contents"></textarea>
     </td>
    </tr> 
    <tr>
     <td colspan="2" align="left" >
      <img src="${initParam.root }/img/write_btn.jpg" alt="글입력" style="cursor:hand"
     id="writeBtn">

     </td>
  
    </tr>
   </table>
  </form>
  </c:otherwise>
</c:choose>
</body>
</html>